package com.xjrsoft.module.customer.erp_model.eRP_PurchaseReceiptInfo.service.impl;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xjrsoft.common.dbmodel.LocalDb;
import com.xjrsoft.common.dbmodel.utils.DataSourceUtil;
import com.xjrsoft.common.helper.DbHelper;
import com.xjrsoft.common.helper.DbHelperFactory;
import com.xjrsoft.common.result.Response;
import com.xjrsoft.core.tool.utils.StringUtil;
import com.xjrsoft.module.customer.erp_model.eRP_PurchaseReceiptInfo.dto.ErpPurchasereceiptentryDto;
import com.xjrsoft.module.customer.erp_model.eRP_PurchaseReceiptInfo.entity.ErpPurchasereceipt;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.xjrsoft.module.customer.erp_model.eRP_PurchaseReceiptInfo.entity.ErpPurchasereceiptentry;
import com.xjrsoft.module.customer.erp_model.eRP_PurchaseReceiptInfo.mapper.ErpPurchasereceiptentryMapper;
import com.xjrsoft.module.customer.erp_model.eRP_PurchaseReceiptInfo.dto.ErpPurchasereceiptListDto;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.common.page.PageOutput;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import com.xjrsoft.module.customer.erp_model.eRP_PurchaseReceiptInfo.mapper.ErpPurchasereceiptMapper;
import com.xjrsoft.module.customer.erp_model.eRP_PurchaseReceiptInfo.service.IErpPurchasereceiptService;
import com.xjrsoft.core.mp.base.BaseService;
import com.xjrsoft.module.customer.erp_model.eRP_PurchaseReceiptInfo.vo.ErpPurchasereceiptentryVo;
import com.xjrsoft.module.customer.erp_model.erpPurchaseOrderInfo.entity.ErpPurchaseorder;
import com.xjrsoft.module.customer.erp_model.erpPurchaseOrderInfo.entity.ErpPurchaseorderentry;
import com.xjrsoft.module.customer.erp_model.erpPurchaseOrderInfo.service.IErpPurchaseorderService;
import com.xjrsoft.module.customer.erp_model.erpPurchaseOrderInfo.service.IErpPurchaseorderentryService;
import com.xjrsoft.module.customer.erp_model.erp_inventoryManage.entity.ErpInventory;
import com.xjrsoft.module.customer.erp_model.erp_inventoryManage.service.IErpInventoryService;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;

/**
 *  服务实现类
 *
 * @author pp
 * @since 2021-09-24
 */
@Service
@AllArgsConstructor
public class ErpPurchasereceiptServiceImpl extends BaseService<ErpPurchasereceiptMapper, ErpPurchasereceipt> implements IErpPurchasereceiptService {

	private ErpPurchasereceiptentryMapper erpPurchasereceiptentryMapper;

	//使用其他service类的时候要注入
	private final IErpInventoryService erpInventoryService;
	private final IErpPurchaseorderService erpPurchaseorderService;

	private  final IErpPurchaseorderentryService erpPurchaseorderentryService;



	@Override
	public IPage<ErpPurchasereceipt> getPageList(ErpPurchasereceiptListDto pageListDto) {
		Wrapper<ErpPurchasereceipt> wrapper = Wrappers.<ErpPurchasereceipt>query().lambda()				.eq(!StringUtil.isEmpty(pageListDto.getF_ReceiptNumber()), ErpPurchasereceipt::getFReceiptnumber, pageListDto.getF_ReceiptNumber())
				.ge(!StringUtil.isEmpty(pageListDto.getF_ReceiptDate_Start()), ErpPurchasereceipt::getFReceiptdate, pageListDto.getF_ReceiptDate_Start())
				.le(!StringUtil.isEmpty(pageListDto.getF_ReceiptDate_End()), ErpPurchasereceipt::getFReceiptdate, pageListDto.getF_ReceiptDate_End())
				.like(!StringUtil.isEmpty(pageListDto.getF_PurchasePeople()), ErpPurchasereceipt::getFPurchasepeople, pageListDto.getF_PurchasePeople())
				.eq(!StringUtil.isEmpty(pageListDto.getF_PurOrderNumber()), ErpPurchasereceipt::getFPurordernumber, pageListDto.getF_PurOrderNumber())
				.like(!StringUtil.isEmpty(pageListDto.getF_DeliveryUnit()), ErpPurchasereceipt::getFDeliveryunit, pageListDto.getF_DeliveryUnit())
				.eq(!StringUtil.isEmpty(pageListDto.getF_DefaultWarehouse()), ErpPurchasereceipt::getFDefaultwarehouse, pageListDto.getF_DefaultWarehouse())
				.like(!StringUtil.isEmpty(pageListDto.getF_Description()), ErpPurchasereceipt::getFDescription, pageListDto.getF_Description())
				.eq(!StringUtil.isEmpty(pageListDto.getF_CreatorUserId()), ErpPurchasereceipt::getFCreatoruserid, pageListDto.getF_CreatorUserId())
				.ge(!StringUtil.isEmpty(pageListDto.getF_CreatorTime_Start()), ErpPurchasereceipt::getFCreatortime, pageListDto.getF_CreatorTime_Start())
				.le(!StringUtil.isEmpty(pageListDto.getF_CreatorTime_End()), ErpPurchasereceipt::getFCreatortime, pageListDto.getF_CreatorTime_End());
		return this.page(ConventPage.getPage(pageListDto), wrapper);
	}

	@SneakyThrows
	@Override
	public String addErpPurchasereceipt(ErpPurchasereceipt erpPurchasereceipt, List<ErpPurchasereceiptentryDto> erpPurchasereceiptentryDtoList,List<ErpPurchasereceiptentry> erpPurchasereceiptentryList, String fPurchaseOrderId, String isModifyStatus) {
		String erpPurchasereceiptId = IdWorker.get32UUID();
		List<ErpPurchaseorderentry> erpPurchaseorderentryList=new ArrayList<ErpPurchaseorderentry>();
		ErpPurchaseorder erpPurchaseorder=new ErpPurchaseorder();
		for (int i=0;i<erpPurchasereceiptentryList.size();i++)
		{
			ErpPurchaseorderentry erpPurchaseorderentry=new ErpPurchaseorderentry();
		    //入库单位和基本单位和库存显示单位都一样可以直接做操作  1.对库存做操作（新增或修改）
            if(erpPurchasereceiptentryList.get(i).getFBasicunit().equals(erpPurchasereceiptentryList.get(i).getFUnit())&&erpPurchasereceiptentryList.get(i).getFComunit().equals(erpPurchasereceiptentryList.get(i).getFUnit()))
			{
				//根据商品id和仓库id查询是否有此商品的库存数据
				String sql1="SELECT * FROM erp_inventory i where i.F_WarehouseId='"+erpPurchasereceipt.getFDefaultwarehouse()+ "' and i.F_GoodsId='"+erpPurchasereceiptentryList.get(i).getFGoodsid()+"'";
				 List<Entity> isInventory1=Db.use(DataSourceUtil.getDataSource("localDB")).query(sql1);
                ErpInventory erpInventory1=new ErpInventory();
				 //如果有的话就修改对应商品的库存
				if (isInventory1.size()>0)
				{
					//修改数量=原有的数据+本次入库的数量
                    BigDecimal aftBasicQua= isInventory1.get(0).getBigDecimal("f_basicamount");
                    BigDecimal aftComamQua=isInventory1.get(0).getBigDecimal("f_comamount");
					BigDecimal receiptQua=new BigDecimal(erpPurchasereceiptentryList.get(i).getFReceiptquantity());
					erpInventory1.setFId(isInventory1.get(0).get("f_id").toString());//根据id修改 一定呀加这句
					erpInventory1.setFGoodsid(erpPurchasereceiptentryList.get(i).getFGoodsid());
					erpInventory1.setFWarehouseid(erpPurchasereceipt.getFDefaultwarehouse());
					erpInventory1.setFBasicamount(aftBasicQua.add(receiptQua));
					erpInventory1.setFComamount(aftComamQua.add(receiptQua));
					//怎么在一个serveimp里去调用另外一个里面的方法 一定要注入
					erpInventoryService.updateById(erpInventory1);
					erpPurchaseorderentry.setFOrderid(fPurchaseOrderId);
					erpPurchaseorderentry.setFGoodsid(erpPurchasereceiptentryList.get(i).getFGoodsid());
					erpPurchaseorderentry.setFId(erpPurchasereceiptentryList.get(i).getFOrderentrtyid());
					//未入库数量=总数量-（已入库数量+本次入库数量）
					erpPurchaseorderentry.setFNoReceiptQua(new BigDecimal(erpPurchasereceiptentryList.get(i).getFQuantity()).subtract(new BigDecimal(erpPurchasereceiptentryDtoList.get(i).getFReceiptQua()).add(new BigDecimal(erpPurchasereceiptentryList.get(i).getFReceiptquantity()))).intValue());
					//已入库数量=总数量-未入库数量
					erpPurchaseorderentry.setFReceiptQua(new BigDecimal(erpPurchasereceiptentryList.get(i).getFQuantity()).subtract(new BigDecimal(erpPurchaseorderentry.getFNoReceiptQua())).intValue());
					erpPurchaseorderentryList.add(erpPurchaseorderentry);
				}
				else {
				 //如果没有的话,就做新增操作
                    erpInventory1.setFGoodsid(erpPurchasereceiptentryList.get(i).getFGoodsid());
                    erpInventory1.setFWarehouseid(erpPurchasereceipt.getFDefaultwarehouse());
                    erpInventory1.setFBasicamount(new BigDecimal(erpPurchasereceiptentryList.get(i).getFReceiptquantity()));
                    erpInventory1.setFComamount(new BigDecimal(erpPurchasereceiptentryList.get(i).getFReceiptquantity()));
                    erpInventoryService.addErpInventory(erpInventory1);
					erpPurchaseorderentry.setFOrderid(fPurchaseOrderId);
					erpPurchaseorderentry.setFGoodsid(erpPurchasereceiptentryList.get(i).getFGoodsid());
					erpPurchaseorderentry.setFNoReceiptQua(erpPurchasereceiptentryDtoList.get(i).getFNoReceiptQua());
					erpPurchaseorderentry.setFReceiptQua(erpPurchasereceiptentryDtoList.get(i).getFReceiptQua());
					erpPurchaseorderentryList.add(erpPurchaseorderentry);
				}
			}
			//不一样先查询换算率得到之后转换
			else {
			    //这里组合有四种情况  只有同时满足不相等的情况 才需要同时转换
                //基本单位转换率
                BigDecimal baseConverRate = null;
                //显示单位转换率
                BigDecimal comConverRate=null;
                if(!erpPurchasereceiptentryList.get(i).getFBasicunit().equals(erpPurchasereceiptentryList.get(i).getFUnit()) &&!erpPurchasereceiptentryList.get(i).getFComunit().equals(erpPurchasereceiptentryList.get(i).getFUnit()))
                {
                        //查询转换率
                        String sql="select c.F_Id,c.F_ConvertRate,c.F_FullName,c.F_ConvertId,c.F_Description from erp_unitconvertchild c left join erp_unitconvert u on u.F_Id=c.F_ConvertId where c.F_ConvertId='"+erpPurchasereceiptentryList.get(i).getFUnit()+"'";
                        List<Entity>  list= Db.use(DataSourceUtil.getDataSource("localDB")).query(sql);

                        for (int j=0;j<list.size();j++)
                        {
                            if (list.get(j).get("f_fullname").toString().equals(erpPurchasereceiptentryList.get(i).getFComunit().toString()))
                            {
                            	comConverRate=new BigDecimal(list.get(j).get("F_ConvertRate").toString());
                            }
                            if (list.get(j).get("f_fullname").toString().equals(erpPurchasereceiptentryList.get(i).getFBasicunit().toString()))
                            {
                                baseConverRate=new BigDecimal(list.get(j).get("F_ConvertRate").toString());
                            }
                        }
                        //得到转换率之后开始转换
                    //根据商品id和仓库id查询是否有此商品的库存数据
                    String sql1="SELECT * FROM erp_inventory i where i.F_WarehouseId='"+erpPurchasereceipt.getFDefaultwarehouse()+ "' and i.F_GoodsId='"+erpPurchasereceiptentryList.get(i).getFGoodsid()+"'";
                    List<Entity> isInventory1=Db.use(DataSourceUtil.getDataSource("localDB")).query(sql1);
                    ErpInventory erpInventory1=new ErpInventory();
                    BigDecimal receiptQua=new BigDecimal(erpPurchasereceiptentryList.get(i).getFReceiptquantity());
                    //如果有的话就修改对应商品的库存
                    if (isInventory1.size()>0)
                    {
						//修改数量=原有的数据+本次入库的数量*转换率
						BigDecimal aftBasicQua= isInventory1.get(0).getBigDecimal("f_basicamount");
						BigDecimal aftComamQua=isInventory1.get(0).getBigDecimal("f_comamount");
                        erpInventory1.setFId(isInventory1.get(0).get("f_id").toString());//根据id修改 一定呀加这句
                        erpInventory1.setFGoodsid(erpPurchasereceiptentryList.get(i).getFGoodsid());
                        erpInventory1.setFWarehouseid(erpPurchasereceipt.getFDefaultwarehouse());
                        erpInventory1.setFBasicamount(aftBasicQua.add(receiptQua.multiply(baseConverRate)));
                        erpInventory1.setFComamount(aftComamQua.add(receiptQua.multiply(comConverRate)));
                        erpInventoryService.updateById(erpInventory1);
                        erpPurchaseorderentry.setFId(erpPurchasereceiptentryList.get(i).getFOrderentrtyid());
						erpPurchaseorderentry.setFOrderid(fPurchaseOrderId);
						erpPurchaseorderentry.setFGoodsid(erpPurchasereceiptentryList.get(i).getFGoodsid());
						//未入库数量=总数量-（已入库数量+本次入库数量）
						erpPurchaseorderentry.setFNoReceiptQua(new BigDecimal(erpPurchasereceiptentryDtoList.get(i).getFQuantity()).subtract(new BigDecimal(erpPurchasereceiptentryDtoList.get(i).getFReceiptQua()).add(new BigDecimal(erpPurchasereceiptentryDtoList.get(i).getFReceiptquantity()))).intValue());
						//已入库数量=总数量-未入库数量
						erpPurchaseorderentry.setFReceiptQua(new BigDecimal(erpPurchasereceiptentryDtoList.get(i).getFQuantity()).subtract(new BigDecimal(erpPurchaseorderentry.getFNoReceiptQua())).intValue());
						erpPurchaseorderentryList.add(erpPurchaseorderentry);
                    }
                    else {
                        //如果没有的话,就做新增操作
                        erpInventory1.setFGoodsid(erpPurchasereceiptentryList.get(i).getFGoodsid());
                        erpInventory1.setFWarehouseid(erpPurchasereceipt.getFDefaultwarehouse());
                        erpInventory1.setFBasicamount(receiptQua.multiply(baseConverRate));
                        erpInventory1.setFComamount(receiptQua.multiply(comConverRate));
                        //如果本次入库数量=0 那么库存应该不变
						if(receiptQua.toString().equals("0"))
						{
							erpInventory1.setFBasicamount(isInventory1.get(0).getBigDecimal("f_basicamount"));
							erpInventory1.setFComamount(isInventory1.get(0).getBigDecimal("f_comamount"));
						}
                        erpInventoryService.addErpInventory(erpInventory1);
						erpPurchaseorderentry.setFOrderid(fPurchaseOrderId);
						erpPurchaseorderentry.setFId(erpPurchasereceiptentryList.get(i).getFOrderentrtyid());
						erpPurchaseorderentry.setFGoodsid(erpPurchasereceiptentryList.get(i).getFGoodsid());
						erpPurchaseorderentry.setFReceiptQua(erpPurchasereceiptentryDtoList.get(i).getFReceiptQua()+erpPurchasereceiptentryDtoList.get(i).getFReceiptquantity());
						erpPurchaseorderentry.setFNoReceiptQua(erpPurchasereceiptentryDtoList.get(i).getFQuantity()-erpPurchaseorderentry.getFReceiptQua());
						erpPurchaseorderentryList.add(erpPurchaseorderentry);
                    }
                }
                else {
                    //其中一个需要转换 先判断是基本数量单位需要转换还是库存显示单位需要转换
					//如果库存显示单位和入库单位不相等 就需要转换,否则就是基本单位需要转换
					//先查询出转换率
					String sql="select c.F_Id,c.F_ConvertRate,c.F_FullName,c.F_ConvertId,c.F_Description from erp_unitconvertchild c left join erp_unitconvert u on u.F_Id=c.F_ConvertId where c.F_ConvertId='"+erpPurchasereceiptentryList.get(i).getFUnit()+"'";
					List<Entity>  list= Db.use(DataSourceUtil.getDataSource("localDB")).query(sql);

					for (int j=0;j<list.size();j++)
					{
						if (list.get(j).get("f_fullname").toString().equals(erpPurchasereceiptentryList.get(i).getFComunit().toString()))
						{
							comConverRate=new BigDecimal(list.get(j).get("F_ConvertRate").toString());
						}
						if (list.get(j).get("f_fullname").toString().equals(erpPurchasereceiptentryList.get(i).getFBasicunit().toString()))
						{
							baseConverRate=new BigDecimal(list.get(j).get("F_ConvertRate").toString());
						}
					}
					//得到转换率之后开始转换
					//根据商品id和仓库id查询是否有此商品的库存数据
					String sql1="SELECT * FROM erp_inventory i where i.F_WarehouseId='"+erpPurchasereceipt.getFDefaultwarehouse()+ "' and i.F_GoodsId='"+erpPurchasereceiptentryList.get(i).getFGoodsid()+"'";
					List<Entity> isInventory1=Db.use(DataSourceUtil.getDataSource("localDB")).query(sql1);
					ErpInventory erpInventory1=new ErpInventory();
					//本次入库数量
					BigDecimal receiptQua=new BigDecimal(erpPurchasereceiptentryList.get(i).getFReceiptquantity());
					//修改数量=原有的数据+本次入库的数量*转换率
					//如果有的话就修改对应商品的库存
					if (isInventory1.size()>0)
					{
						BigDecimal aftBasicQua=isInventory1.get(0).getBigDecimal("f_basicamount");
						BigDecimal aftComamQua=isInventory1.get(0).getBigDecimal("f_comamount");
						erpInventory1.setFId(isInventory1.get(0).get("f_id").toString());//根据id修改 一定呀加这句
						erpInventory1.setFGoodsid(erpPurchasereceiptentryList.get(i).getFGoodsid());
						erpInventory1.setFWarehouseid(erpPurchasereceipt.getFDefaultwarehouse());
						erpInventory1.setFBasicamount(aftBasicQua.add(receiptQua));
						erpInventory1.setFComamount(aftComamQua.add(receiptQua));
						//是否需要转换 需要的话就重新赋值
						if (!erpPurchasereceiptentryList.get(i).getFComunit().equals(erpPurchasereceiptentryList.get(i).getFUnit()))
						{
							erpInventory1.setFComamount(aftComamQua.add(receiptQua.multiply(comConverRate)));
						}
						else
						{
							erpInventory1.setFBasicamount(aftBasicQua.add(receiptQua.multiply(baseConverRate)));
						}
						erpInventoryService.updateById(erpInventory1);
						erpPurchaseorderentry.setFId(erpPurchasereceiptentryList.get(i).getFOrderentrtyid());
						erpPurchaseorderentry.setFOrderid(fPurchaseOrderId);
						erpPurchaseorderentry.setFGoodsid(erpPurchasereceiptentryList.get(i).getFGoodsid());
						//未入库数量=总数量-（已入库数量+本次入库数量）
						erpPurchaseorderentry.setFNoReceiptQua(new BigDecimal(erpPurchasereceiptentryDtoList.get(i).getFQuantity()).subtract(new BigDecimal(erpPurchasereceiptentryDtoList.get(i).getFReceiptQua()).add(new BigDecimal(erpPurchasereceiptentryDtoList.get(i).getFReceiptquantity()))).intValue());
						//已入库数量=总数量-未入库数量
						erpPurchaseorderentry.setFReceiptQua(new BigDecimal(erpPurchasereceiptentryDtoList.get(i).getFQuantity()).subtract(new BigDecimal(erpPurchaseorderentry.getFNoReceiptQua())).intValue());
						erpPurchaseorderentryList.add(erpPurchaseorderentry);
					}
					else {
						//如果没有的话,就做新增操作
						erpInventory1.setFGoodsid(erpPurchasereceiptentryList.get(i).getFGoodsid());
						erpInventory1.setFWarehouseid(erpPurchasereceipt.getFDefaultwarehouse());
						erpInventory1.setFBasicamount(receiptQua);
						erpInventory1.setFComamount(receiptQua);
						//是否需要转换 需要的话就重新赋值
						if (!erpPurchasereceiptentryList.get(i).getFComunit().equals(erpPurchasereceiptentryList.get(i).getFUnit()))
						{
							erpInventory1.setFComamount(receiptQua.multiply(comConverRate));
						}
						else
						{
							erpInventory1.setFBasicamount(receiptQua.multiply(baseConverRate));
						}
						erpInventoryService.addErpInventory(erpInventory1);
						erpPurchaseorderentry.setFId(erpPurchasereceiptentryList.get(i).getFOrderentrtyid());
						erpPurchaseorderentry.setFOrderid(fPurchaseOrderId);
						erpPurchaseorderentry.setFGoodsid(erpPurchasereceiptentryList.get(i).getFGoodsid());
						//未入库数量=总数量-（已入库数量+本次入库数量）
						erpPurchaseorderentry.setFNoReceiptQua(new BigDecimal(erpPurchasereceiptentryDtoList.get(i).getFQuantity()).subtract(new BigDecimal(erpPurchasereceiptentryDtoList.get(i).getFReceiptQua()).add(new BigDecimal(erpPurchasereceiptentryDtoList.get(i).getFReceiptquantity()))).intValue());
						//已入库数量=总数量-未入库数量
						erpPurchaseorderentry.setFReceiptQua(new BigDecimal(erpPurchasereceiptentryDtoList.get(i).getFQuantity()).subtract(new BigDecimal(erpPurchaseorderentry.getFNoReceiptQua())).intValue());
						erpPurchaseorderentryList.add(erpPurchaseorderentry);
					}
                }
			}
		}
		//2.对采购订单的商品数量做修改（已入库数量、未入库数量）
		erpPurchaseorder.setFId(fPurchaseOrderId);
		erpPurchaseorderentryService.updateBatchById(erpPurchaseorderentryList);
		//3.对采购订单的状态做操作 根据前端传来的值是否要修改订单的状态,只有所有的本次入库数量+已入库数量等于或>总数量才修改  否则代表可能进行二次入库
		if(isModifyStatus.equals("true"))
		{
				erpPurchaseorder.setFId(fPurchaseOrderId);
				erpPurchaseorder.setFStatus(2);
				erpPurchaseorderService.updateById(erpPurchaseorder);
		}
		else {
			erpPurchaseorder.setFId(fPurchaseOrderId);
			erpPurchaseorder.setFStatus(0);
			erpPurchaseorderService.updateById(erpPurchaseorder);
		}
		erpPurchasereceipt.setFInvalidmark(1);
		erpPurchasereceipt.setFAuditmark(1);
		erpPurchasereceipt.setFPurorderid(fPurchaseOrderId);
		boolean isSuccess = this.save(erpPurchasereceipt);
		erpPurchasereceiptentryList.forEach(erpPurchasereceiptentry -> {
			erpPurchaseorderentryList.forEach(erpPurchaseorderentry -> {
				if (erpPurchaseorderentry.getFGoodsid() == erpPurchasereceiptentry.getFGoodsid())
				{
					erpPurchasereceiptentry.setFReceiptqua(erpPurchaseorderentry.getFReceiptQua());
					erpPurchasereceiptentry.setFNoreceiptqua(erpPurchaseorderentry.getFNoReceiptQua());
				}
			});
			erpPurchasereceiptentry.setFReceiptid(erpPurchasereceipt.getFId());
		});
		this.saveBatch(erpPurchasereceiptentryList, ErpPurchasereceiptentry.class, ErpPurchasereceiptentryMapper.class);
		return isSuccess ? erpPurchasereceiptId : null;
	}

	@SneakyThrows
	@Override
	public boolean updateErpPurchasereceipt(String id,ErpPurchasereceipt erpPurchasereceipt, List<ErpPurchasereceiptentryDto> erpPurchasereceiptentryDtoList,List<ErpPurchasereceiptentry> erpPurchasereceiptentryList, String fPurchaseOrderId, String isModifyStatus) {
		List<ErpPurchaseorderentry> erpPurchaseorderentryList=new ArrayList<ErpPurchaseorderentry>();
		ErpPurchaseorder erpPurchaseorder=new ErpPurchaseorder();
		for (int i=0;i<erpPurchasereceiptentryList.size();i++)
		{
			ErpPurchaseorderentry erpPurchaseorderentry=new ErpPurchaseorderentry();
			//入库单位和基本单位和库存显示单位都一样可以直接做操作  1.对库存做操作（新增或修改）
			if(erpPurchasereceiptentryList.get(i).getFBasicunit().equals(erpPurchasereceiptentryList.get(i).getFUnit())&&erpPurchasereceiptentryList.get(i).getFComunit().equals(erpPurchasereceiptentryList.get(i).getFUnit()))
			{
				//根据商品id和仓库id查询是否有此商品的库存数据
				String sql1="SELECT * FROM erp_inventory i where i.F_WarehouseId='"+erpPurchasereceipt.getFDefaultwarehouse()+ "' and i.F_GoodsId='"+erpPurchasereceiptentryList.get(i).getFGoodsid()+"'";
				List<Entity> isInventory1=Db.use(DataSourceUtil.getDataSource("localDB")).query(sql1);
				ErpInventory erpInventory1=new ErpInventory();
				//如果有的话就修改对应商品的库存
				if (isInventory1.size()>0)
				{
					//修改数量=原有的数据+本次入库的数量
					BigDecimal aftBasicQua= isInventory1.get(0).getBigDecimal("f_basicamount");
					BigDecimal aftComamQua=isInventory1.get(0).getBigDecimal("f_comamount");
					BigDecimal receiptQua=new BigDecimal(erpPurchasereceiptentryList.get(i).getFReceiptquantity());
					erpInventory1.setFId(isInventory1.get(0).get("f_id").toString());//根据id修改 一定呀加这句
					erpInventory1.setFGoodsid(erpPurchasereceiptentryList.get(i).getFGoodsid());
					erpInventory1.setFWarehouseid(erpPurchasereceipt.getFDefaultwarehouse());
					erpInventory1.setFBasicamount(aftBasicQua.add(receiptQua));
					erpInventory1.setFComamount(aftComamQua.add(receiptQua));
					//怎么在一个serveimp里去调用另外一个里面的方法 一定要注入
					erpInventoryService.updateById(erpInventory1);
					erpPurchaseorderentry.setFOrderid(fPurchaseOrderId);
					erpPurchaseorderentry.setFGoodsid(erpPurchasereceiptentryList.get(i).getFGoodsid());
					erpPurchaseorderentry.setFId(erpPurchasereceiptentryList.get(i).getFOrderentrtyid());
					//未入库数量=总数量-（已入库数量+本次入库数量）
					erpPurchaseorderentry.setFNoReceiptQua(new BigDecimal(erpPurchasereceiptentryList.get(i).getFQuantity()).subtract(new BigDecimal(erpPurchasereceiptentryDtoList.get(i).getFReceiptQua()).add(new BigDecimal(erpPurchasereceiptentryList.get(i).getFReceiptquantity()))).intValue());
					//已入库数量=总数量-未入库数量
					erpPurchaseorderentry.setFReceiptQua(new BigDecimal(erpPurchasereceiptentryList.get(i).getFQuantity()).subtract(new BigDecimal(erpPurchaseorderentry.getFNoReceiptQua())).intValue());
					erpPurchaseorderentryList.add(erpPurchaseorderentry);
				}
				else {
					//如果没有的话,就做新增操作
					erpInventory1.setFGoodsid(erpPurchasereceiptentryList.get(i).getFGoodsid());
					erpInventory1.setFWarehouseid(erpPurchasereceipt.getFDefaultwarehouse());
					erpInventory1.setFBasicamount(new BigDecimal(erpPurchasereceiptentryList.get(i).getFReceiptquantity()));
					erpInventory1.setFComamount(new BigDecimal(erpPurchasereceiptentryList.get(i).getFReceiptquantity()));
					erpInventoryService.addErpInventory(erpInventory1);
					erpPurchaseorderentry.setFOrderid(fPurchaseOrderId);
					erpPurchaseorderentry.setFGoodsid(erpPurchasereceiptentryList.get(i).getFGoodsid());
					erpPurchaseorderentry.setFNoReceiptQua(erpPurchasereceiptentryDtoList.get(i).getFNoReceiptQua());
					erpPurchaseorderentry.setFReceiptQua(erpPurchasereceiptentryDtoList.get(i).getFReceiptQua());
					erpPurchaseorderentryList.add(erpPurchaseorderentry);
				}
			}
			//不一样先查询换算率得到之后转换
			else {
				//这里组合有四种情况  只有同时满足不相等的情况 才需要同时转换
				//基本单位转换率
				BigDecimal baseConverRate = null;
				//显示单位转换率
				BigDecimal comConverRate=null;
				if(!erpPurchasereceiptentryList.get(i).getFBasicunit().equals(erpPurchasereceiptentryList.get(i).getFUnit()) &&!erpPurchasereceiptentryList.get(i).getFComunit().equals(erpPurchasereceiptentryList.get(i).getFUnit()))
				{
					//查询转换率
					String sql="select c.F_Id,c.F_ConvertRate,c.F_FullName,c.F_ConvertId,c.F_Description from erp_unitconvertchild c left join erp_unitconvert u on u.F_Id=c.F_ConvertId where c.F_ConvertId='"+erpPurchasereceiptentryList.get(i).getFUnit()+"'";
					List<Entity>  list= Db.use(DataSourceUtil.getDataSource("localDB")).query(sql);

					for (int j=0;j<list.size();j++)
					{
						if (list.get(j).get("f_fullname").toString().equals(erpPurchasereceiptentryList.get(i).getFComunit().toString()))
						{
							comConverRate=new BigDecimal(list.get(j).get("F_ConvertRate").toString());
						}
						if (list.get(j).get("f_fullname").toString().equals(erpPurchasereceiptentryList.get(i).getFBasicunit().toString()))
						{
							baseConverRate=new BigDecimal(list.get(j).get("F_ConvertRate").toString());
						}
					}
					//得到转换率之后开始转换
					//根据商品id和仓库id查询是否有此商品的库存数据
					String sql1="SELECT * FROM erp_inventory i where i.F_WarehouseId='"+erpPurchasereceipt.getFDefaultwarehouse()+ "' and i.F_GoodsId='"+erpPurchasereceiptentryList.get(i).getFGoodsid()+"'";
					List<Entity> isInventory1=Db.use(DataSourceUtil.getDataSource("localDB")).query(sql1);
					ErpInventory erpInventory1=new ErpInventory();
					BigDecimal receiptQua=new BigDecimal(erpPurchasereceiptentryList.get(i).getFReceiptquantity());
					//如果有的话就修改对应商品的库存
					if (isInventory1.size()>0)
					{
						//修改数量=原有的数据+本次入库的数量*转换率
						BigDecimal aftBasicQua= isInventory1.get(0).getBigDecimal("f_basicamount");
						BigDecimal aftComamQua=isInventory1.get(0).getBigDecimal("f_comamount");
						erpInventory1.setFId(isInventory1.get(0).get("f_id").toString());//根据id修改 一定呀加这句
						erpInventory1.setFGoodsid(erpPurchasereceiptentryList.get(i).getFGoodsid());
						erpInventory1.setFWarehouseid(erpPurchasereceipt.getFDefaultwarehouse());
						erpInventory1.setFBasicamount(aftBasicQua.add(receiptQua.multiply(baseConverRate)));
						erpInventory1.setFComamount(aftComamQua.add(receiptQua.multiply(comConverRate)));
						erpInventoryService.updateById(erpInventory1);
						erpPurchaseorderentry.setFId(erpPurchasereceiptentryList.get(i).getFOrderentrtyid());
						erpPurchaseorderentry.setFOrderid(fPurchaseOrderId);
						erpPurchaseorderentry.setFGoodsid(erpPurchasereceiptentryList.get(i).getFGoodsid());
						//未入库数量=总数量-（已入库数量+本次入库数量）
						erpPurchaseorderentry.setFNoReceiptQua(new BigDecimal(erpPurchasereceiptentryDtoList.get(i).getFQuantity()).subtract(new BigDecimal(erpPurchasereceiptentryDtoList.get(i).getFReceiptQua()).add(new BigDecimal(erpPurchasereceiptentryDtoList.get(i).getFReceiptquantity()))).intValue());
						//已入库数量=总数量-未入库数量
						erpPurchaseorderentry.setFReceiptQua(new BigDecimal(erpPurchasereceiptentryDtoList.get(i).getFQuantity()).subtract(new BigDecimal(erpPurchaseorderentry.getFNoReceiptQua())).intValue());
						erpPurchaseorderentryList.add(erpPurchaseorderentry);
					}
					else {
						//如果没有的话,就做新增操作
						erpInventory1.setFGoodsid(erpPurchasereceiptentryList.get(i).getFGoodsid());
						erpInventory1.setFWarehouseid(erpPurchasereceipt.getFDefaultwarehouse());
						erpInventory1.setFBasicamount(receiptQua.multiply(baseConverRate));
						erpInventory1.setFComamount(receiptQua.multiply(comConverRate));
						//如果本次入库数量=0 那么库存应该不变
						if(receiptQua.toString().equals("0"))
						{
							erpInventory1.setFBasicamount(isInventory1.get(0).getBigDecimal("f_basicamount"));
							erpInventory1.setFComamount(isInventory1.get(0).getBigDecimal("f_comamount"));
						}
						erpInventoryService.addErpInventory(erpInventory1);
						erpPurchaseorderentry.setFOrderid(fPurchaseOrderId);
						erpPurchaseorderentry.setFId(erpPurchasereceiptentryList.get(i).getFOrderentrtyid());
						erpPurchaseorderentry.setFGoodsid(erpPurchasereceiptentryList.get(i).getFGoodsid());
						erpPurchaseorderentry.setFReceiptQua(erpPurchasereceiptentryDtoList.get(i).getFReceiptQua()+erpPurchasereceiptentryDtoList.get(i).getFReceiptquantity());
						erpPurchaseorderentry.setFNoReceiptQua(erpPurchasereceiptentryDtoList.get(i).getFQuantity()-erpPurchaseorderentry.getFReceiptQua());
						erpPurchaseorderentryList.add(erpPurchaseorderentry);
					}
				}
				else {
					//其中一个需要转换 先判断是基本数量单位需要转换还是库存显示单位需要转换
					//如果库存显示单位和入库单位不相等 就需要转换,否则就是基本单位需要转换
					//先查询出转换率
					String sql="select c.F_Id,c.F_ConvertRate,c.F_FullName,c.F_ConvertId,c.F_Description from erp_unitconvertchild c left join erp_unitconvert u on u.F_Id=c.F_ConvertId where c.F_ConvertId='"+erpPurchasereceiptentryList.get(i).getFUnit()+"'";
					List<Entity>  list= Db.use(DataSourceUtil.getDataSource("localDB")).query(sql);

					for (int j=0;j<list.size();j++)
					{
						if (list.get(j).get("f_fullname").toString().equals(erpPurchasereceiptentryList.get(i).getFComunit().toString()))
						{
							comConverRate=new BigDecimal(list.get(j).get("F_ConvertRate").toString());
						}
						if (list.get(j).get("f_fullname").toString().equals(erpPurchasereceiptentryList.get(i).getFBasicunit().toString()))
						{
							baseConverRate=new BigDecimal(list.get(j).get("F_ConvertRate").toString());
						}
					}
					//得到转换率之后开始转换
					//根据商品id和仓库id查询是否有此商品的库存数据
					String sql1="SELECT * FROM erp_inventory i where i.F_WarehouseId='"+erpPurchasereceipt.getFDefaultwarehouse()+ "' and i.F_GoodsId='"+erpPurchasereceiptentryList.get(i).getFGoodsid()+"'";
					List<Entity> isInventory1=Db.use(DataSourceUtil.getDataSource("localDB")).query(sql1);
					ErpInventory erpInventory1=new ErpInventory();
					//本次入库数量
					BigDecimal receiptQua=new BigDecimal(erpPurchasereceiptentryList.get(i).getFReceiptquantity());
					//修改数量=原有的数据+本次入库的数量*转换率
					//如果有的话就修改对应商品的库存
					if (isInventory1.size()>0)
					{
						BigDecimal aftBasicQua=isInventory1.get(0).getBigDecimal("f_basicamount");
						BigDecimal aftComamQua=isInventory1.get(0).getBigDecimal("f_comamount");
						erpInventory1.setFId(isInventory1.get(0).get("f_id").toString());//根据id修改 一定呀加这句
						erpInventory1.setFGoodsid(erpPurchasereceiptentryList.get(i).getFGoodsid());
						erpInventory1.setFWarehouseid(erpPurchasereceipt.getFDefaultwarehouse());
						erpInventory1.setFBasicamount(aftBasicQua.add(receiptQua));
						erpInventory1.setFComamount(aftComamQua.add(receiptQua));
						//是否需要转换 需要的话就重新赋值
						if (!erpPurchasereceiptentryList.get(i).getFComunit().equals(erpPurchasereceiptentryList.get(i).getFUnit()))
						{
							erpInventory1.setFComamount(aftComamQua.add(receiptQua.multiply(comConverRate)));
						}
						else
						{
							erpInventory1.setFBasicamount(aftBasicQua.add(receiptQua.multiply(baseConverRate)));
						}
						erpInventoryService.updateById(erpInventory1);
						erpPurchaseorderentry.setFId(erpPurchasereceiptentryList.get(i).getFOrderentrtyid());
						erpPurchaseorderentry.setFOrderid(fPurchaseOrderId);
						erpPurchaseorderentry.setFGoodsid(erpPurchasereceiptentryList.get(i).getFGoodsid());
						//未入库数量=总数量-（已入库数量+本次入库数量）
						erpPurchaseorderentry.setFNoReceiptQua(new BigDecimal(erpPurchasereceiptentryDtoList.get(i).getFQuantity()).subtract(new BigDecimal(erpPurchasereceiptentryDtoList.get(i).getFReceiptQua()).add(new BigDecimal(erpPurchasereceiptentryDtoList.get(i).getFReceiptquantity()))).intValue());
						//已入库数量=总数量-未入库数量
						erpPurchaseorderentry.setFReceiptQua(new BigDecimal(erpPurchasereceiptentryDtoList.get(i).getFQuantity()).subtract(new BigDecimal(erpPurchaseorderentry.getFNoReceiptQua())).intValue());
						erpPurchaseorderentryList.add(erpPurchaseorderentry);
					}
					else {
						//如果没有的话,就做新增操作
						erpInventory1.setFGoodsid(erpPurchasereceiptentryList.get(i).getFGoodsid());
						erpInventory1.setFWarehouseid(erpPurchasereceipt.getFDefaultwarehouse());
						erpInventory1.setFBasicamount(receiptQua);
						erpInventory1.setFComamount(receiptQua);
						//是否需要转换 需要的话就重新赋值
						if (!erpPurchasereceiptentryList.get(i).getFComunit().equals(erpPurchasereceiptentryList.get(i).getFUnit()))
						{
							erpInventory1.setFComamount(receiptQua.multiply(comConverRate));
						}
						else
						{
							erpInventory1.setFBasicamount(receiptQua.multiply(baseConverRate));
						}
						erpInventoryService.addErpInventory(erpInventory1);
						erpPurchaseorderentry.setFId(erpPurchasereceiptentryList.get(i).getFOrderentrtyid());
						erpPurchaseorderentry.setFOrderid(fPurchaseOrderId);
						erpPurchaseorderentry.setFGoodsid(erpPurchasereceiptentryList.get(i).getFGoodsid());
						//未入库数量=总数量-（已入库数量+本次入库数量）
						erpPurchaseorderentry.setFNoReceiptQua(new BigDecimal(erpPurchasereceiptentryDtoList.get(i).getFQuantity()).subtract(new BigDecimal(erpPurchasereceiptentryDtoList.get(i).getFReceiptQua()).add(new BigDecimal(erpPurchasereceiptentryDtoList.get(i).getFReceiptquantity()))).intValue());
						//已入库数量=总数量-未入库数量
						erpPurchaseorderentry.setFReceiptQua(new BigDecimal(erpPurchasereceiptentryDtoList.get(i).getFQuantity()).subtract(new BigDecimal(erpPurchaseorderentry.getFNoReceiptQua())).intValue());
						erpPurchaseorderentryList.add(erpPurchaseorderentry);
					}
				}
			}
		}
		//2.对采购订单的商品数量做修改（已入库数量、未入库数量）
		erpPurchaseorder.setFId(fPurchaseOrderId);
		erpPurchaseorderentryService.updateBatchById(erpPurchaseorderentryList);
		//3.对采购订单的状态做操作 根据前端传来的值是否要修改订单的状态,只有所有的本次入库数量+已入库数量等于或>总数量才修改成已入库  否则代表可能进行二次入库 就是部分入库状态
		if(isModifyStatus.equals("true"))
		{
			erpPurchaseorder.setFId(fPurchaseOrderId);
			erpPurchaseorder.setFStatus(2);
		}
		else {
			erpPurchaseorder.setFId(fPurchaseOrderId);
			erpPurchaseorder.setFStatus(0);
		}
		erpPurchaseorderService.updateById(erpPurchaseorder);
		erpPurchasereceipt.setFId(id);
		erpPurchasereceiptentryList.forEach(erpPurchasereceiptentry -> {
			erpPurchaseorderentryList.forEach(erpPurchaseorderentry -> {
				if (erpPurchaseorderentry.getFGoodsid() == erpPurchasereceiptentry.getFGoodsid())
				{
					erpPurchasereceiptentry.setFReceiptqua(erpPurchaseorderentry.getFReceiptQua());
					erpPurchasereceiptentry.setFNoreceiptqua(erpPurchaseorderentry.getFNoReceiptQua());
				}
			});
			erpPurchasereceiptentry.setFReceiptid(id);
		});
		erpPurchasereceiptentryMapper.delete(Wrappers.<ErpPurchasereceiptentry>query().lambda().eq(ErpPurchasereceiptentry::getFReceiptid, id));
		this.saveBatch(erpPurchasereceiptentryList, ErpPurchasereceiptentry.class, ErpPurchasereceiptentryMapper.class);
		return this.updateById(erpPurchasereceipt);
	}

	public List<ErpPurchasereceiptentryVo> getErpPurchasereceiptentryByParentId(String parentId){
		return erpPurchasereceiptentryMapper.getList(parentId);
	}
}
